<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset = utf-8"/>
		<title>WebSocket客户端</title>
	<script type="text/javascript">
		var socket;
		if(!window.WebSocket){
			window.WebSocket = window.MozWebSocket;
		}

		if(window.WebSocket){
		//var url="ws://localhost:8888/schat_websocket";
		//http://test.17doubao.cn/schat
		var url="ws://test.17doubao.cn/schat_websoket/schat_websoket";
		//var url="ws://101.200.87.16:9206/schat_websocket"
			socket = new WebSocket(url);
			socket.onmessage = function(event){
				var ta = document.getElementById('responseContent');
				ta.value += event.data + "\r\n";
			};

			socket.onopen = function(event){
				var ta = document.getElementById('responseContent');
				ta.value = "你当前的浏览器支持WebSocket,请进行后续操作\r\n";
				setInterval(function(){
				var msg="{\"contentType\":2,\"toUserType\":\"1\",\"status\":\"heartbeat\"}";
					socket.send(msg);
				},10000);
			};

			socket.onclose = function(event){
				var ta = document.getElementById('responseContent');
				ta.value = "";
				ta.value = "WebSocket连接已经关闭\r\n";
			};
		}else{
			alert("您的浏览器不支持WebSocket");
		}


		function send(message){
			if(!window.WebSocket){
				return;
			}
			if(socket.readyState == WebSocket.OPEN){
				socket.send(message);
			}else{
				alert("WebSocket连接没有建立成功！！");
			}
		}
	</script>
	</head>
	<body>
		<form onSubmit="return false;">
			<!-- {"from":"123","to": "321","contentType":1,"content":"你好"} -->
			<h2>发送的报文</h2>
			<textarea id = "msg" name="msg" style = "width:1024px; height:500px"></textarea>
			<br/><br/>
			<input type = "button" value = "发送WebSocket请求消息" onClick = "send(this.form.msg.value)"/>
			<hr color="red"/>
			<h2>客户端接收到服务端返回的应答消息</h2>
			<textarea id = "responseContent" style = "width:1024px; height:300px"></textarea>
		</form>
	</body>
</html>